package server

import (
	"errors"
	"fmt"
	"github.com/spf13/cast"
	"longmen/server/config/global"
	"longmen/server/pkg/common/api_models"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/db/mysql/db_live"
	"longmen/server/pkg/db/mysql/db_sports"
	"longmen/server/pkg/protobuf"
	"strings"
)

func getEsMachList(req *protobuf.Get55MachListReq) (*protobuf.Get55MachListResp, error) {
	//阻止短时间大量访问, 每秒限制
	maxReqLimit := 60
	if util.NeedIPRateLimit("esMach", maxReqLimit) {
		global.Log.Errorf("getEsMachList|访问次数超过限制 :%v", maxReqLimit)
		return nil, errors.New("访问次数超过限制")
	}

	resp := new(protobuf.Get55MachListResp)
	var matchs []*api_models.EsMatchTempSchedule
	if req.ReqSource == 1 {
		matchs = db_sports.Get55MatchEsList(global.GetEs(), int(req.GetSportId()), req.GetComp(), int(req.GetMatchState()), int64(req.GetMatchDate()), req.GetPlatform())
	} else {
		matchs = db_sports.GetSeoMatchEsList(global.GetEs(), int(req.GetSportId()), req.GetComp(), int(req.GetMatchState()), int64(req.GetMatchDate()))
	}
	if len(matchs) > 0 {
		var total int32
		var matchIds []int
		var fEsTmpIds []interface{}
		var bEsTmpIds []interface{}
		var footScoresMap map[int]*mergeFootScore
		var basketScoresMap map[int]*mergeBasketScore
		for _, v := range matchs {
			if v.MatchStatus == 1 {
				matchIds = append(matchIds, v.MatchId)
				if v.SportId == db_sports.BasketBallSportId { //篮球
					bEsTmpIds = append(bEsTmpIds, v.MatchId)
				} else if v.SportId == db_sports.FootBallSportId { //足球
					fEsTmpIds = append(fEsTmpIds, v.MatchId)
				}

			}
		}
		var lives []*protobuf.LiveInfo
		if len(matchIds) > 0 {
			lives, _ = db_live.GetLivePbParamForIds(matchIds)
			if len(fEsTmpIds) > 0 {
				footScores := db_sports.EsFootballScore(fEsTmpIds, len(fEsTmpIds))
				footScoresMap = mergeFootMatchScore(footScores)
			}
			if len(bEsTmpIds) > 0 {
				basketScores := db_sports.EsBasketballScore(bEsTmpIds, len(bEsTmpIds))
				basketScoresMap = mergeBasketMatchScore(basketScores)
			}

		}
		for _, v := range matchs {
			var anchorIds string
			if v.MatchStatus == 1 {
				for _, l := range lives {
					if v.MatchId == int(l.Matchid) {
						anchorIds += fmt.Sprintf("%s,", cast.ToString(l.Anchorid))
					}
				}
				if len(anchorIds) > 0 {
					anchorIds = strings.TrimRight(anchorIds, ",")
				}
			}
			matchesSuggest := &protobuf.Matches55Suggest{
				Away:              v.Away,
				AwayScore:         v.AwayScore,
				AwayLog:           v.AwayLogo,
				Comp:              v.Comp,
				Home:              v.Home,
				HomeScore:         v.HomeScore,
				HomeLog:           v.HomeLogo,
				CompLog:           v.CompLogo,
				MatchDetailStatus: v.MatchDetailStatus,
				MatchTime:         v.MatchTime,
				TimePlayerd:       v.TimePlayed,
				AnchorIds:         anchorIds,
				SportId:           uint32(v.SportId),
				MatchId:           uint32(v.MatchId),
				RawMatchStatus:    int32(v.RawMatchStatus),
				CompShortZh:       v.CompShortZh,
			}
			if v.SportId == db_sports.FootBallSportId {
				if _, ok := footScoresMap[v.MatchId]; ok {
					matchesSuggest.FHomeScore = footScoresMap[v.MatchId].Home
					matchesSuggest.FAwayScore = footScoresMap[v.MatchId].Away
				}

			} else if v.SportId == db_sports.BasketBallSportId {
				if _, ok := basketScoresMap[v.MatchId]; ok {
					matchesSuggest.BHomeScore = basketScoresMap[v.MatchId].Home
					matchesSuggest.BAwayScore = basketScoresMap[v.MatchId].Away
				}
			}
			if v.MatchStatus == db_sports.MatchStaring {
				resp.Staring = append(resp.Staring, matchesSuggest)
				total++
			}
			if v.MatchStatus == db_sports.MatchPending {
				resp.Pending = append(resp.Pending, matchesSuggest)
				total++
			}
			if v.MatchStatus == db_sports.MatchFinished {
				resp.Finished = append(resp.Finished, matchesSuggest)
				total++
			}
		}
		resp.Total = total
	}
	return resp, nil
}
